<?php

/**
 * Page() contains data about a page
*/
class Page {

	# Store details in an array
	private $p_id;
	//var $page_data = array();
	private $p_name;
	private $p_parent;
	private $p_accesstype;
	private $p_access;
	private $p_accessdatestart;
	private $p_accessdateend;
	private $p_created;
	private $p_modified;
	private $p_accessaction;
	private $p_status;
	private $p_section;
	private $p_sort;
	private $p_content;
	private $p_custom_content;
	private $p_innavigation;
	private $p_popup;
	private $p_path;
	private $p_layer;
	
	/**
	*construtor of this class
	*
	*/
	function __construct() {
			
		$this -> p_id = "";
		$this -> p_name = "";
		$this -> p_parent = "";
		$this -> p_accesstype = 0;
		$this -> p_access = array();
		$this -> p_accessdatestart = "";
		$this -> p_accessdateend = ""; 
		$this -> p_created = "";
		$this -> p_modified = "";
		$this -> p_accessaction = "";
		$this -> p_status = 0;
		$this -> p_section = "";
		$this -> p_sort = 0;
		$this -> p_content = "";
		$this -> p_custom_content = "";
		$this -> p_innavigation = 0;
		$this -> p_popup = 0;
		$this -> p_path = array();
		$this -> p_children = array();
		$this -> p_layer = "";
	}
   
   	/**
	*destructor of this class
	*
	*/
   	function __destruct(){
		
		#destroy
		unset($this -> p_id);
		unset($this -> p_name);
		unset($this -> p_parent);
		unset($this -> p_accesstype);
		unset($this -> p_access);
		unset($this -> p_accessdatestart);
		unset($this -> p_accessdateend); 
		unset($this -> p_created);
		unset($this -> p_modified);
		unset($this -> p_accessaction);
		unset($this -> p_status);
		unset($this -> p_section);
		unset($this -> p_sort);
		unset($this -> p_content);
		unset($this -> p_custom_content);
		unset($this -> p_innavigation);
		unset($this -> p_popup);
		unset($this -> p_path);
		unset($this -> p_children);
		unset($this -> p_layer);
   	}
	
	
	/**
	*loadPageData() load sql data into page class
	*@param $arrPage is the sql data array 
	*/
	public function loadPageData($arrPage){
		
		$flag = false;//flag to show whether current page exist
 
		#get user id from given user array
		if(array_key_exists('p_id',$arrPage)){
			
			if($this -> p_id == $arrPage['p_id']){
				
				$flag = true;
			}
			else{
				
				$this -> p_id = $arrPage['p_id'];
			}
		}
			
		if(array_key_exists('p_name',$arrPage) && !$flag)
			$this -> p_name = $arrPage['p_name'];
			
		if(array_key_exists('p_parent',$arrPage) && !$flag)
			$this -> p_parent = $arrPage['p_parent'];
		
		if(array_key_exists('p_accesstype',$arrPage) && !$flag)
			$this -> p_accesstype = $arrPage['p_accesstype'];			

		#find group id of current user
		if(array_key_exists('g_id',$arrPage)){

			$this -> p_access[$arrPage['g_id']] = $arrPage['g_id'];
		}
		
		if(array_key_exists('p_accessdatestart',$arrPage) && !$flag){
			
			$this -> p_accessdatestart = $arrPage['p_accessdatestart'];
		}
		
		if(array_key_exists('p_accessdateend',$arrPage) && !$flag){
			
			$this -> p_accessdateend = $arrPage['p_accessdateend'];
		}
		
		if(array_key_exists('p_created',$arrPage) && !$flag){
			
			$this -> p_created = $arrPage['p_created'];
		}
		
		if(array_key_exists('p_modified',$arrPage) && !$flag){
			
			$this -> p_modified = $arrPage['p_modified'];
		}
		
		if(array_key_exists('p_accessaction',$arrPage) && !$flag){
			
			$this -> p_accessaction = $arrPage['p_accessaction'];
		}
		
		if(array_key_exists('p_status',$arrPage) && !$flag){
			
			$this -> p_status = $arrPage['p_status'];
		}
		
		if(array_key_exists('p_section',$arrPage) && !$flag){
			
			$this -> p_section = $arrPage['p_section'];
		}
		
		if(array_key_exists('p_sort',$arrPage) && !$flag){
			
			$this -> p_sort = $arrPage['p_sort'];
		}
		
		if(array_key_exists('p_content',$arrPage) && !$flag){
			
			$this -> p_content = $arrPage['p_content'];
		}
		
		if(array_key_exists('p_custom_content',$arrPage) && !$flag){
			
			$this -> p_custom_content = $arrPage['p_custom_content'];
		}
		
		if(array_key_exists('p_innavigation',$arrPage) && !$flag){
			
			$this -> p_innavigation = $arrPage['p_innavigation'];
		}
		
		if(array_key_exists('p_popup',$arrPage) && !$flag){
			
			$this -> p_popup = $arrPage['p_popup'];
		}
		
		//if(array_key_exists('p_popup',$arrPage) && !$flag){
//			
//			$this -> p_popup = $arrPage['p_popup'];
//		}
		
		if(array_key_exists('pp_path',$arrPage)){
			
			if(!empty($arrPage['pp_path'])){
				
				if(array_key_exists('p_layer_2',$arrPage)){
					
					$this -> p_path[$arrPage['p_layer_2']] = array('path' => $arrPage['pp_path'],'layer'=>$arrPage['p_layer_2']);
				}
				else{
			
					$this -> p_path[$arrPage['p_layer_2']] = array('path' => $arrPage['pp_path']);
				}
			}
		}
		
		if(array_key_exists('pp_p_id',$arrPage)){
			
			if(!empty($arrPage['pp_p_id']) && $arrPage['pp_p_id'] != $arrPage['p_id']){
			
				$this -> p_children[$arrPage['pp_p_id']] = $arrPage['pp_p_id'];
			}
		}
		
		if(array_key_exists('p_layer',$arrPage) && !$flag){
			
			$this -> p_layer = $arrPage['p_layer'];
		}
	}
	
	

	///**
//	 * Page() instantiates the Page object
//	 *
//	 * @param page_id is the ID of the page
//	 * @param base_data is the base data to be added to the page
//	*/
//	function Page($page_id = "", $base_data = array()) {
//
//		# Global
//		global $PAGES;
//
//		# Check ID providedd
//		if ($page_id != "") {
//
//			# Set ID
//			$this -> page_id = $page_id;
//
//			# For each bit of data
//			foreach ($base_data as $data_key => $data_value) {
//
//				# Add to array
//				$this -> addData($data_key, $data_value);
//			}
//		}
//	}
//
	/**
	 * addData() adds a piece of data
	 *
	 * @param data_key is the Key for the data
	 * @param data_value is the Value for the data
	*/
	function addData($data_key = "", $data_value = "") {

		# Ensure key passed and doesn't already exist
		if ($data_key != "" && !isset($this -> page_data[$data_key])) {

			# Add
			$this -> page_data[$data_key] = $data_value;
		}
	}

	/**
	 * removeData() removes a piece of data
	 *
	 * @param data_key is the Key of the data to remove
	*/
	function removeData($data_key = "") {

		# If value passed
		if ($data_key != "") {

			# Remove
		}
	}

	/**
	 * getData() gets a piece of data
	 *
	 * @param data_key is the Key of the data to get
	 *
	 * @return the data, or false if not found
	*/
	function getData($data_key = "") {
		
		$data = "";
		
		switch($data_key){
			
			case 'p_name':$data = $this -> p_name;break;
			
			case 'p_parent':$data = $this -> p_parent;break;
			
			case 'p_accesstype':$data = $this -> p_accesstype;break;
			
			case 'p_access':$data = $this -> p_access;break;
			
			case 'g_id':$data = $this -> g_id;break;
			
			case 'p_accessdatestart':$data = $this -> p_accessdatestart;break;
			
			case 'p_accessdateend':$data = $this -> p_accessdateend;break;
			
			case 'p_created':$data = $this -> p_created;break;
			
			case 'p_modified':$data = $this -> p_modified;break;
			
			case 'p_accessaction':$data = $this -> p_accessaction;break;
			
			case 'p_status':$data = $this -> p_status;break;
			
			case 'p_section':$data = $this -> p_section;break;
		
			case 'p_sort':$data = $this -> p_sort;break;
			
			case 'p_content':$data = $this -> p_content;break;
			
			case 'p_custom_content':$data = $this -> p_custom_content;break;
			
			case 'p_innavigation':$data = $this -> p_innavigation;break;
			
			case 'p_popup':$data = $this -> p_popup;break;
			
			case 'p_path':$data = $this -> p_path;break;
			
			case 'p_children':$data = $this -> p_children;break;
			
			case 'p_layer':$data = $this -> p_layer;break;
		}

		return $data;
	}

	/**
	 * getID() gets the ID of the page
	 *
	 * @return the ID of the page
	*/
	function getID() {

		# Return
		return $this -> p_id;
	}

	/**
	 * printPage() prints out the page data
	 *
	 * @return a print out of the page
	*/
	function printPage() {

		# Output
		$output = "<h2>Page: '" . $this -> page_id . "'</h2>\n<pre><ul>";

		# Params
		foreach ($this -> page_data as $data_key => $data_value) {

			$output .= "<li>" . $data_key . " => " . $data_value . "</li>";
		}
		
		# End
		$output .= "</ul></pre>";

		# Return
		return $output;
	}
}

?>